package com.enterprise.projectManager.resource;

import com.enterprise.projectManager.model.Contract;
import com.enterprise.projectManager.model.Response;
import com.enterprise.projectManager.service.implemention.ContractImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import static java.time.LocalDateTime.now;
import static java.util.Map.of;
import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.OK;

@RestController
@RequestMapping("/crebas/contract")
@RequiredArgsConstructor
public class ContractResource {
    private final ContractImpl contractService;
    // 创建一个新的合同
    @PostMapping("/createContract")
    public ResponseEntity<Response> createContract(@RequestBody Contract contract) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("newContract", contractService.createcontra(contract)))
                        .message("新建合同")
                        .status(CREATED)
                        .statusCode(CREATED.value())
                        .build()
        );
    }
        // 删除一个合同
        @DeleteMapping("/deleteContract/{id1}")
        public ResponseEntity<Response> deleteContract(@PathVariable String id1) {
            return ResponseEntity.ok(
                    Response.builder()
                            .timeStamp(now())
                            .data(of("deleteResult",contractService.deletecontra(id1)))
                            .message("删除合同")
                            .status(OK)
                            .statusCode(OK.value())
                            .build()
            );
        }
    // 更新一个合同
    @PostMapping("/updateContract")
    public ResponseEntity<Response> updateContract(@RequestBody Contract contract) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("updatedContract",contractService.updatecontra(contract)))
                        .message("更新合同")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }


    // 列出所有合同
    @GetMapping("/listContract/{limitcon}")
    public ResponseEntity<Response> listContracts(@PathVariable int limitcon) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("contracts",contractService.listcontra(limitcon)))
                        .message("获取"+limitcon+"个合同")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 列出所有合同
    @GetMapping("/listAll")
    public ResponseEntity<Response> listAll() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("contracts",contractService.listAll()))
                        .message("获取所有合同")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 获取所有甲方
    @GetMapping("/getAllFirstCompany")
    public ResponseEntity<Response> getAllFirstCompany() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("contracts",contractService.findAllDistinctFirstCompany()))
                        .message("查询所有甲方")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 获取所有乙方
    @GetMapping("/getAllSecondCompany")
    public ResponseEntity<Response> getAllSecondCompany() {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("contracts",contractService.findAllDistinctSecondCompany()))
                        .message("查询所有乙方")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }

    // 过滤
    @PostMapping("/filterContract")
    public ResponseEntity<Response> filterCondition(@RequestBody Contract contract) {
        return ResponseEntity.ok(
                Response.builder()
                        .timeStamp(now())
                        .data(of("filterContract",contractService.filterContract(contract)))
                        .message("查找合同")
                        .status(OK)
                        .statusCode(OK.value())
                        .build()
        );
    }
}
